<template>
    <el-dialog :title="isEdit?'编辑库房设置':'新增库房设置'" :visible.sync="dialogForm" width="680px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="110px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="仓库代码" prop="storeCode">
                        <el-input v-model="input" placeholder=""></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <!-- <el-form-item label="仓库名称" prop="plateNumber">
                        <el-input v-model="form.plateNumber" clearable></el-input>
                    </el-form-item> -->
                    <el-form-item label="仓库名称" prop="storeName">
                        <el-input v-model="input" placeholder=""></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="仓库类型" prop="storeType">
                        <auto-search-dic @select="handleCllx"
                                         @selectName="v=>form.typeName=v"
                                         @selectId="v=>form.typeId=v"
                                         :name="form.typeName"
                                         code="JOB_CLLX"></auto-search-dic>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属单位" prop="orgId">
                        <auto-search-org v-model="form.orgId"
                                         @select="v=>{form.orgId=v;form.deptId='';form.deptName=''}"
                                         @selectName="v=>form.orgName=v"
                                         :disabled="true"
                                         :name="form.orgName"></auto-search-org>
                        <!-- {{$store.state.user.currentUser.org.name}} -->
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="所在地" prop="address">
                        <el-input v-model="input" placeholder=""></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="联系电话" prop="tel">
                        <el-input v-model="input" placeholder=""></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="联系人" prop="concat">
                        <el-input v-model="input" placeholder=""></el-input>
                    </el-form-item>
                </el-col>       
                <el-col :span="12">
                    <el-form-item label="备注" prop="remark">
                        <el-input type="textarea" :rows="3" style="width: 188px" placeholder="请输入内容"
                                  :maxLength="$maxLength"
                                  v-model="form.resourceDescribe"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addJobVehicle, updateJobVehicle } from "@/api/baseResource/baseResource";
    import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
    import autoSearchDic from '@/components/autocomplete/autoSearchDic'
    import autoSearchTer from '@/components/autocomplete/autoSearchTer'
    import searchDept from '@/components/autocomplete/autoSearchDept'
    import { maxLength, sortLength } from "@/utils/rules";
    import { PUBLIC_LIMIT, REG_ALL_NUMBER, PUBLIC_NUMBER_NULL } from "@/utils/Reg";
    export default {
        name: "adminWarehouseAddForm",
        components: { autoSearchOrg,searchDept,autoSearchDic,autoSearchTer },
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    storeCode: [{ required: true, validator:PUBLIC_LIMIT, trigger: 'blur' },sortLength],
                    storeName: [{ required: true, validator:PUBLIC_LIMIT, trigger: 'blur' },sortLength],
                    storeType: [{ required: true, message:'请选择仓库类型', trigger: 'blur' },sortLength],
                    // orgId: [{ message:'请选择单位', trigger: 'change' }],
                    // address: [{ validator:PUBLIC_LIMIT, trigger: 'blur' },sortLength],
                    // tel: [{ validator:PUBLIC_LIMIT, trigger: 'blur' }],
                    // terminalName: [{ required: true, message:'请选择对接终端', trigger: 'blur' },sortLength],
                    // concat: [{ message:'请选择单位', trigger: 'change' }],
                    // remark: [maxLength]
                },
                dialogForm: false,
                isEdit: false,
            }
        },
        methods: {
            clearForm() {
                return {
                    storeCode:"",
                    storeName:"",
                    storeType:"",
                    orgId:"",
                    address:"",
                    tel:"",
                    terminalName:"",
                    concat:"",
                    remark:"",
                    orgId:this.$store.state.user.currentUser.org&&this.$store.state.user.currentUser.org.id,
                    orgName:this.$store.state.user.currentUser.org&&this.$store.state.user.currentUser.org.name
                }
            },
            // 保存
            handleSubmit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.isEdit ? this.handleToEdit() : this.handleToAdd()
                    } else {
                        this.$message({
                            message: '请检查表单',
                            type: 'error'
                        })
                    }
                })
            },
            // 新增
            handleToAdd() {
                addJobVehicle(this.form).then(res => {
                    this.$message({ message: '新增成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 编辑
            handleToEdit() {
                updateJobVehicle(this.form.id, this.form).then(res => {
                    this.$message({ message: '编辑成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            handleCllx(v){
                if(v==='JOB_CLLX_HC'){
                    this.form.teu=0
                }
                this.form.typeCode=v
            }
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$refs.form.resetFields()
                    this.isEdit = false
                    this.form = this.clearForm()
                    this.$emit('clear')
                }
            },
        },
    }
</script>

<style scoped>

</style>
